<template>
  <div class="twoxm-cate-item">
      <router-link tag="div" :to="`/detail?id=${id}`">
        <img :src="image" alt="">
        <p class="twoxm-cate-item-title">{{ title }}</p>
        <p class="twoxm-cate-item-content">
          <span>￥{{ price | toFix2 }}</span>
          <span>销量:{{ saleNum }}</span>
        </p>
      </router-link>
          <p
          class="twoxm-cate-item-btn"
          @click="addToCart({
            id,
            img:image,
            title,
            price
          })"
          >加入购物车</p>
  </div>
</template>

<script>
import { createNamespacedHelpers } from 'vuex'
const cart = createNamespacedHelpers('cart')
export default {
  name: 'cateitem',
  props: ['id', 'image', 'title', 'price', 'saleNum'],
  methods: {
    ...cart.mapMutations(['addToCart'])
  }
}
</script>

<style lang="scss" scoped>
.twoxm-cate-item {
   width: 50%;
  padding-top: 4%;
  &:nth-child(odd) {
    padding-left: 4%;
    padding-right: 2%;
  }
  &:nth-child(even) {
    padding-left: 2%;
    padding-right: 4%;
  }
  box-sizing: border-box;
  img {
    width: 100%;
  }
   &-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    line-height: 2;
  }
  &-content {
    display: flex;
    justify-content: space-between;
     span:first-child {
      font-size: 14px;
      color: #fa585a;
    }
    span:last-child {
      font-size: 12px;
      color: #000;
    }
  }
  &-btn {
    width: 100%;
    line-height: 2;
    margin-top: 5px;
    background-color: #26a2ff;
    color: #fff;
    text-align: center;
    border-radius: 5px;
  }
}
</style>
